package com.qitianyong.extendUI;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewParent;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;


/**
 * Created by qitianyong on 2017/3/2. 滑动循环展示视图
 */

public class ViewsCircle extends HorizontalScrollView {

    private int __viewWidth = 0;
    private int __viewHeight = 0;
    private int __viewsCountofScreen = 0; // 一屏显示View个数
    private LinearLayout __container; // 使用liearlayout作为容器

    private int __nextViewIndex = 0; // 左移动时的预准备View系数
    private CircleAdapter __adapter;

    public ViewsCircle(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        __container = (LinearLayout) getChildAt(0);
    }

    // 左边滑动，准备下一个view
    protected void PrepareNextView(){
        // 删除容器第一个View
        scrollTo(0, 0);
        __container.removeViewAt(0);

        if(__nextViewIndex < 0){
            __nextViewIndex += __adapter.getCount();
        }

        // 把预备View添加到容器里
        View view = __adapter.getView(__nextViewIndex, null, __container);

        ViewParent vp = view.getParent();
        if(vp != null){
            __container.removeView(view);
        }

        __container.addView(view);

        // 计算下一个预备View的序号
        __nextViewIndex++;
        if(__nextViewIndex >= __adapter.getCount()){
            __nextViewIndex %= __adapter.getCount();
        }
    }

    // 右边滑动，准备上一个view
    protected  void PreparePreviousView(){

        // 计算下一个预备View的序号index
        if(__nextViewIndex < 0){
            __nextViewIndex = __adapter.getCount()+__nextViewIndex;
        }

        int index = __nextViewIndex% __adapter.getCount() - __viewsCountofScreen;
        if(index < 0){
            index = __adapter.getCount()+index;
        }

        // 删除容器中最后一个View
        int lastViewPos = __container.getChildCount() - 1;
        __container.removeViewAt(lastViewPos);
        View view = __adapter.getView(index, null, __container);

        // 查看是否在容器里
        ViewParent vp = view.getParent();
        if(vp != null){
            __container.removeView(view);
        }

        __container.addView(view, 0);

        scrollTo(__viewWidth, 0);
        __nextViewIndex--;
    }

    // 设置适配器
    public void setAdapter(CircleAdapter adapter){
        __adapter = adapter;

        // 加入一个View进入容器，以便计算View的宽高
        View view = __adapter.getView(0, null, __container);

        __container = (LinearLayout) getChildAt(0);
        __container.addView(view);

        if(__viewHeight == 0 && __viewWidth == 0){

            // 计算宽高
            int w = View.MeasureSpec.makeMeasureSpec(0,
                    View.MeasureSpec.UNSPECIFIED);
            int h = View.MeasureSpec.makeMeasureSpec(0,
                    View.MeasureSpec.UNSPECIFIED);

            view.measure(w, h);

            __viewHeight = view.getMeasuredHeight();
            __viewWidth = view.getMeasuredWidth();
        }
    }

    // 设置一屏幕显示View个数
    public void setScreen(int screen_w){

        // 清理所有的View
        __container = (LinearLayout) getChildAt(0);
        __container.removeAllViews();

        __viewsCountofScreen =
                (screen_w / __viewWidth == 0)?screen_w/__viewWidth+1:screen_w/__viewWidth+2;
        for (int i=0; i<__viewsCountofScreen; i++)
        {
            View view = __adapter.getView(i, null, __container);

            // 偏移到此位置添加View
            __container.addView(view);
            __nextViewIndex = i;
        }

        Log.i("item", "index:"+__nextViewIndex+" screen width:"+screen_w);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {

            case MotionEvent.ACTION_MOVE: {
                int  maxScrollX = getChildAt(0).getMeasuredWidth()-getMeasuredWidth();
                int scrollX = getScrollX();

                if (scrollX == 0) {
                    PreparePreviousView();
                }else if (scrollX == maxScrollX) {
                    PrepareNextView();
                }
                break;
            }
            default:break;
        }

        return super.onTouchEvent(ev);
    }
}
